
/**
 * This class provide methods for creating control message 
 * which is to be communicated between client and server.
 * 
 * @author pkulkar@ncsu.edu
 */
package com.android.networking.message;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.StringTokenizer;

import android.util.Log;

public class ControlMessage implements Serializable{
	ControlPacket localPack;
	public ControlMessage(String destinationIP,int destinationPort,String muticastIP,int score,String playerMode,int lengthOfExtraData,String extraData){
		try{
		    localPack = new ControlPacket();
		    localPack.setDestinationIP(destinationIP);
		    localPack.setDestinationPort(destinationPort);
		    localPack.setSourceIP(InetAddress.getLocalHost().getHostAddress());
		    localPack.setSourcePort(destinationPort);
		    localPack.setExtraData(extraData);
		    localPack.setLengthOfExtraData(lengthOfExtraData);
		    localPack.setMuticastIP(muticastIP);
		    localPack.setScore(score);
		    localPack.setPlayerMode(playerMode);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public ControlPacket getControlPacket(){
		return localPack;
	}
	public void createNewConnection(){
		try{
			localPack.setRequestType(ControlRquestType.NEW_CONNECTION);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void assignMulticastID(){
		try{
			localPack.setRequestType(ControlRquestType.MUTICAST_ID_ASSIGNEMT);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void clientClose(){
		try{
			localPack.setRequestType(ControlRquestType.CLIENT_CLOSE);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void clientCloseAck(){
		try{
			localPack.setRequestType(ControlRquestType.CLIENT_CLOSE_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void serverClose(){
		try{
			localPack.setRequestType(ControlRquestType.SERVER_CLOSE);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void serverCloseAck(){
		try{
			localPack.setRequestType(ControlRquestType.SERVER_CLOSE_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void testConnection(){
		try{
			localPack.setRequestType(ControlRquestType.CONNECTION_TEST);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void testConnectionAck(){
		try{
			localPack.setRequestType(ControlRquestType.CONNECTION_TEST_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void reqeustScore(){
		try{
			localPack.setRequestType(ControlRquestType.SCORE_REQUEST);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void reqeustScoreAck(){
		try{
			localPack.setRequestType(ControlRquestType.SCORE_REQUEST_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
		
	public void reqeustScoreResponse(){
		try{
			localPack.setRequestType(ControlRquestType.SCORE_RESPONSE);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void reqeustScoreResponseAck(){
		try{
			localPack.setRequestType(ControlRquestType.SCORE_RESPONSE_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void canvasClear(){
		try{
			localPack.setRequestType(ControlRquestType.CANVAS_CLEAR);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void canvasClearAck(){
		try{
			localPack.setRequestType(ControlRquestType.CANVAS_CLEAR_ACK);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void displayControlMessage(){
		localPack.displayControlPacket();
	}
	
	public String getMuticastIP() {
		return localPack.getMuticastIP();
	}

	public int getScore() {
		return localPack.getScore();
	}

	public int getLengthOfExtraData() {
		return localPack.getLengthOfExtraData();
	}

	public String getExtraData() {
		return localPack.getExtraData();
	}

	public int getRequestType() {
		return localPack.getRequestType();
	}

	public String getSourceIP() {
		return localPack.getSourceIP();
	}

	public int getSourcePort() {
		return localPack.getSourcePort();
	}

	public String getDestinationIP() {
		return localPack.getDestinationIP();
	}

	public int getDestinationPort() {
		return localPack.getDestinationPort();
	}
	
	public String getPlayerMode() {
		return localPack.getPlayerMode();
	}


	public void setReqeustType(int val){
		try{
			localPack.setRequestType(val);
			localPack.computeChecksum();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public String toString(){
		StringBuffer buf = new StringBuffer();
		buf.append("messageType ControlMessage ");
		buf.append("destinationIP ");
		buf.append(localPack.getDestinationIP());
		buf.append("destinationPort ");
		buf.append(localPack.getDestinationPort());
		buf.append("sourceIP ");
		buf.append(localPack.getSourceIP());
		buf.append("sourcePort ");
		buf.append(localPack.getSourcePort());
		buf.append(" requestType ");
		buf.append(localPack.getRequestType());
		buf.append(" playerMode ");
		buf.append(localPack.getPlayerMode());
		buf.append(" multicastIP ");
		buf.append(localPack.getMuticastIP());
		if(localPack.getScore()!=-1){
			buf.append("score ");
			buf.append(localPack.getScore());
		}
		
		return buf.toString();
	}
	
	public static ControlMessage getControlMessageInstance(String str){
		StringTokenizer tok = new StringTokenizer(str);
		String destinationIP = null ,sourceIP=null,playerMode=null,muticastIP=null;
		int destinationPort=-1, sourcePort=-1,score = -1,request=-1;
		while(tok.hasMoreTokens()){
			String name = tok.nextToken();
			 if(name.equals("destinationIP"))
			 	destinationIP = tok.nextToken();
			 else if(name.equals("destinationPort"))
				 destinationPort = (Integer.parseInt(tok.nextToken()));
			 else if(name.equals("sourceIP"))
			 	sourceIP = tok.nextToken();
			 else if(name.equals("sourcePort"))
			 	sourcePort = (Integer.parseInt(tok.nextToken()));
			 else if(name.equals("requestType"))
				 	sourcePort = (Integer.parseInt(tok.nextToken()));
			 else if(name.equals("score"))
			 	score = (Integer.parseInt(tok.nextToken()));
			 else if(name.equals("playerMode"))
				 	playerMode = tok.nextToken();
			 else if(name.equals("multicastIP"))
				 muticastIP = tok.nextToken();
			 else{
				 tok.nextElement();
			 }
		}
		ControlMessage obj = new ControlMessage( destinationIP, destinationPort, muticastIP, score, playerMode, 0, "");
		obj.setReqeustType(request);
		return obj;
	}
}
